


#include "system_param.h"
#include "fault_analyse.h"
#include "fault_TH_detect.h"
#include "offline_param.h"

uint16_t comm_overtime_ms[ENUM_COMM_ALL] = {0};

STR_FAULT_CODE faultCode;

STR_FLT_DIAG vdcOCP;
STR_FLT_DIAG vdcUCP;
STR_FLT_DIAG idcOCP;
STR_FLT_DIAG vdcMotorOCP;
STR_FLT_DIAG vdcMotorUCP;
STR_FLT_DIAG idcMotorOCP;
STR_FLT_DIAG idcCleanOCP;
STR_FLT_DIAG idcServiceOCP;

STR_FLT_DIAG navCommOTP;
STR_FLT_DIAG batCommOTP;
STR_FLT_DIAG cleanCommOTP;


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void fault_param_init(void)
{

	navCommOTP.threshold[0] = AppParamHdl.comm_ot_ms;
	navCommOTP.threshold[1] = AppParamHdl.comm_ot_ms;
	navCommOTP.negate = DIAG_NORMAL;
	navCommOTP.latch  = FAULT_NOT_LATCH;
	navCommOTP.schmidt = AppParamHdl.comm_ot_ms * 0.1f;
	navCommOTP.maxcnt = 0;
	
	batCommOTP.threshold[0] = AppParamHdl.bat_comm_ot_ms;
	batCommOTP.threshold[1] = AppParamHdl.bat_comm_ot_ms;
	batCommOTP.negate = DIAG_NORMAL;
	batCommOTP.latch  = FAULT_NOT_LATCH;
	batCommOTP.schmidt = AppParamHdl.bat_comm_ot_ms * 0.1f;
	batCommOTP.maxcnt = 0;
	
	cleanCommOTP.threshold[0] = AppParamHdl.clean_comm_ot_ms;
	cleanCommOTP.threshold[1] = AppParamHdl.clean_comm_ot_ms;
	cleanCommOTP.negate = DIAG_NORMAL;
	cleanCommOTP.latch  = FAULT_NOT_LATCH;
	cleanCommOTP.schmidt = AppParamHdl.clean_comm_ot_ms * 0.1f;
	cleanCommOTP.maxcnt = 0;
	
	
	

}





/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void set_comm_loss_time(ENUM_COMM_LOSS type, uint16_t loss_ms)
{
	comm_overtime_ms[type] += loss_ms;
	
	if(comm_overtime_ms[type] > COMM_OVERTIME_MAX)
	{
		comm_overtime_ms[type] = COMM_OVERTIME_MAX;
	}
	
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void reset_comm_loss_time(ENUM_COMM_LOSS type)
{
	comm_overtime_ms[type] = 0;
}






/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void comm_overtime_detect(uint16_t runtime_ms)
{
	
	set_comm_loss_time(ENUM_NAV, runtime_ms);
	
	set_comm_loss_time(ENUM_BAT, runtime_ms);
	
	set_comm_loss_time(ENUM_CLEAN, runtime_ms);
	
	#if 0
	thresholdFltDiag(comm_overtime_ms[ENUM_NAV], &navCommOTP, runtime_ms);
	
	thresholdFltDiag(comm_overtime_ms[ENUM_BAT], &batCommOTP, runtime_ms);
	
	if(AppParamHdl.device_type == ENUM_CLEAN_ROBOT)
	{
		thresholdFltDiag(comm_overtime_ms[ENUM_CLEAN], &cleanCommOTP, runtime_ms);		
	}
	#endif
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/









/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void fault_code_update(void)
{
	
	faultCode.bit.nav_comm_ot = navCommOTP.fault;
	
	faultCode.bit.bat_comm_ot = batCommOTP.fault;
	
	faultCode.bit.clean_comm_ot = cleanCommOTP.fault;	

	
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void fault_code_clear(void)
{
	
	reset_comm_loss_time(ENUM_NAV);
	
	reset_comm_loss_time(ENUM_BAT);
	
	reset_comm_loss_time(ENUM_CLEAN);
	
	thresholdFltClear(&navCommOTP);
	
	thresholdFltClear(&batCommOTP);
	
	thresholdFltClear(&cleanCommOTP);
	
	

}









